/**
 * Copyright 2008 Adam Ruggles.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.code.sapien.guice.transaction;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.PersistenceException;
import javax.persistence.RollbackException;

import com.google.code.sapien.exception.TransactionException;
import com.google.inject.Inject;

/**
 * Wraps an EntityManager EntityTransaction.
 * @author Adam
 * @version $Id: DefaultTransaction.java 16 2009-04-12 05:05:02Z a.ruggles $
 * 
 * Created on Oct 9, 2008 at 12:43:05 PM 
 */
public class DefaultTransaction implements Transaction {
	/**
	 * The Entity Manager.
	 */
	private final EntityManager entityManager;

	/**
	 * The Entity Transaction.
	 */
	private final EntityTransaction entityTransaction;

	/**
	 * Constructs a EntityManager Transaction with a EntityTransaction.
	 * @param entityManager The entity manager.
	 */
	@Inject
	public DefaultTransaction(final EntityManager entityManager) {
		if (entityManager == null) {
			throw new IllegalArgumentException("Entity Manager cannot be null");
		}
		this.entityManager = entityManager;
		this.entityTransaction = entityManager.getTransaction();
	}

	/**
	 * {@inheritDoc}
	 * @see kiff.util.transaction.Transaction#begin()
	 */
	public void begin() {
		try {
			entityTransaction.begin();
		} catch (IllegalStateException e) {
			throw new TransactionException("Entity Transaction Begin IllegalStateException", e);
		}
	}

	/**
	 * {@inheritDoc}
	 * @see kiff.util.transaction.Transaction#commit()
	 */
	public void commit() {
		try {
			entityTransaction.commit();
		} catch (IllegalStateException e) {
			throw new TransactionException("Entity Transaction Commit IllegalStateException", e);
		} catch (RollbackException e) {
			throw new TransactionException("Entity Transaction Commit RollbackException", e);
		}
	}

	/**
	 * {@inheritDoc}
	 * @see kiff.util.transaction.Transaction#end()
	 */
	public void end() {
		entityManager.close();
	}

	/**
	 * {@inheritDoc}
	 * @see kiff.util.transaction.Transaction#isRollbackOnly()
	 */
	public boolean isRollbackOnly() {
		try {
			return entityTransaction.getRollbackOnly();
		} catch (IllegalStateException e) {
			throw new TransactionException("Entity Transaction RollbackOnly IllegalStateException", e);
		}
	}

	/**
	 * {@inheritDoc}
	 * @see kiff.util.transaction.Transaction#isActive()
	 */
	public boolean isActive() {
		try {
			return entityTransaction.isActive();
		} catch (PersistenceException e) {
			throw new TransactionException("Entity Transaction isActive PersistenceException", e);
		}
	}

	/**
	 * {@inheritDoc}
	 * @see kiff.util.transaction.Transaction#rollback()
	 */
	public void rollback() {
		try {
			entityTransaction.rollback();
		} catch (IllegalStateException e) {
			throw new TransactionException("Entity Transaction Rollback IllegalStateException", e);
		} catch (PersistenceException e) {
			throw new TransactionException("Entity Transaction Rollback PersistenceException", e);
		}
	}

	/**
	 * {@inheritDoc}
	 * @see kiff.util.transaction.Transaction#setRollbackOnly()
	 */
	public void setRollbackOnly() {
		try {
			entityTransaction.setRollbackOnly();
		} catch (IllegalStateException e) {
			throw new TransactionException("Entity Transaction setRollbackOnly IllegalStateException", e);
		}
	}

	/**
	 * {@inheritDoc}
	 * @see kiff.util.transaction.Transaction#setTransactionTimeout(int)
	 */
	public void setTransactionTimeout(final int seconds) {
		// Not supported in this implementation.  This method is ignored.
	}
}
